package com.yiwei.ydd.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.yiwei.ydd.R;
import com.yiwei.ydd.api.Api;
import com.yiwei.ydd.api.bean.CommissionDetailBean;
import com.yiwei.ydd.api.model.CommissionDetailModel;
import com.yiwei.ydd.base.BaseActivity;
import com.yiwei.ydd.util.DateTime;
import com.yiwei.ydd.util.Network;
import com.yiwei.ydd.util.RxLifeUtil;
import com.yiwei.ydd.util.ToastUtil;
import com.yiwei.ydd.view.V19FrameLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class CommissionDetailActivity extends BaseActivity {


    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.txt_status)
    TextView txtStatus;
    @BindView(R.id.txt_info_left)
    TextView txtInfoLeft;
    @BindView(R.id.txt_info_right)
    TextView txtInfoRight;
    private Unbinder unbiner;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_commission_detail);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {
        txtTitle.setText("佣金明细详情");
        String id = getIntent().getStringExtra("id");
        getCommissionDetails(id);
    }

    public void getCommissionDetails(String id) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }

        CommissionDetailBean bean = new CommissionDetailBean();
        bean.id = id;
        loadingDialog.show();
        Api.api_service.getCommissionDetails(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    setData(result.datas);
                });
    }

    private void setData(CommissionDetailModel.DatasBean datas) {
//        "phone": "136****0671",                 //用户手机号
//                "date": "2018-05-04 00:06:39",          //购买日期
//                "combo": "1000元油卡即时充值",         //购买套餐
//                "money": "1000.00",                     //价格
//                "fee": "10.00",                         //佣金
//                "get_date":null                         //获得日期
//        "status": "1"                           //状态（1：已收，0：未收）
        if (datas.status.equals("0")) {
            txtStatus.setText("未到账钱包");
            txtInfoLeft.setText("好友账号\n充值时间\n充值套餐\n充值资金\n应收佣金");
            String time = "";
            if (TextUtils.isEmpty(datas.date)) {
                time = "";
            } else {
                time = DateTime.toDate(Long.valueOf(datas.date) * 1000);
            }
            txtInfoRight.setText(datas.phone + "\n" + time + "\n" + datas.combo + "\n" + datas.money + "元\n" + datas.fee + "元");
        } else {
            txtStatus.setText("已到账钱包");

            txtInfoLeft.setText("好友账号\n充值时间\n充值套餐\n充值资金\n应收佣金\n到账时间");
            String time = "";
            if (TextUtils.isEmpty(datas.date)) {
                time = "";
            } else {
                time = DateTime.toDate(Long.valueOf(datas.date) * 1000);
            }

            String arrive_time = "";
            if (TextUtils.isEmpty(datas.get_date)) {
                arrive_time = "";
            } else {
                arrive_time = DateTime.toDate(Long.valueOf(datas.get_date) * 1000);
            }
            txtInfoRight.setText(datas.phone + "\n" + time + "\n" + datas.combo + "\n" + datas.money + "元\n" + datas.fee + "元\n"+arrive_time);
        }


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }

    @OnClick(R.id.btn_back)
    public void onViewClicked() {
        finish();
    }
}
